<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>转账</title>
    <link rel="stylesheet" type="text/css" href="/css/basic.css"/>
    <link rel="stylesheet" type="text/css" href="/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="/css/center.css"/>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="/css/public.css"/>
</head>

<body>
<div th:fragment="transfer">

    <div class="ck_nr">
        <div class="tk_nr zz_nr">
            <div class="tk_title" style="padding-top:10px; height:40px;">
                <h3>转账</h3>
                <span>产品钱包和产品钱包之间不能进行互转，请先转入中心钱包后再转到需要转入的产品钱包。(AG娱乐城和捕鱼王共用一个钱包)</span>
            </div>

            <form id="transferForm">
                <!--<div class="tk_box">
                    <div class="zz_nr-pic"></div>
                    <div class="tk_jr">
                        <span class="text">转出钱包：</span><span class="input">
                            <select id="source-transfer" onchange="outSelect()" class="r_inptut inputwd300"
                                    name="sourceTransfer">
                                <option selected value="main">主钱包</option>
                                <option value="HG">锦绣厅</option>
                            </select>
                        </span>
                    </div>
                    <div class="tk_yh">
                        <span class="text">转入钱包：</span><span class="input">
                            <select id="desc-transfer" class="r_inptut inputwd300"
                                    name="descTransfer">
                                <option value="main">主钱包</option>
                                <option selected value="HG">锦绣厅</option>
                            </select>
                        </span>
                    </div>
                    <div class="tk_yh"><span class="text">&nbsp;&nbsp;金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额：</span>
                        <span class="input">
                            <input name="transferAmount" type="number" id="transferMoney" placeholder="金额"
                                   class="r_inptut inputwd300">
                        </span>
                    </div>
                    <div>
                        <button type="button" id="transfer_submit" class="as_but inputwd300">立即转账</button>
                    </div>
                </div>-->

                <div class="tk_box">
                    <div class="zz_nr-pic"></div>
                    <div class="tk_jr">
                        <span class="text" style="color:#000;">转出钱包：</span>
                        <span class="input">
                            <select style="border:1px #000 solid" id="source-transfer" class="r_inptut inputwd300"
                                    name="sourceTransfer">
                                <option selected value="main">主钱包</option>
                                <option value="HG">锦绣厅</option>
                                <option value="PT">小游戏</option>
                                <option value="SB">体育</option>
                            </select>
                        </span>
                    </div>
                    <div class="tk_yh">
                        <span class="text" style="color:#000;">转入钱包：</span><span class="input">
                            <select style="border:1px #000 solid" id="desc-transfer" class="r_inptut inputwd300"
                                    name="descTransfer">
                                <option value="main">主钱包</option>
                                <option selected value="HG">锦绣厅</option>
                                <option value="PT">小游戏</option>
                                <option value="SB">体育</option>
                            </select>
                        </span>
                    </div>
                    <div class="tk_yh"><span class="text" style="color:#000;">　　金额：</span>
                        <span class="input">
                            <input name="transferAmount" type="number" id="transferMoney" placeholder="金额"
                                   class="r_inptut inputwd300">
                        </span>
                    </div>
                    <div class="tk_num clear" style="margin-top: 10px"><span class="money_box" style="margin-left: 105px;"><b>200</b><b>500</b><b>1000</b><b>2000</b></span></div>

                    <div class="tk_yh" style="margin-bottom:40px;margin-top: 10px">
                        <span class="text" style="color:#000;">等级优惠：</span>
                        <span class="input">
                            <select style="border:1px #000 solid" class="r_inptut inputwd300" id="levelDiscounts"
                                    name="levelDiscounts">
                            </select>
                        </span>
                    </div>
                    <div class="yuanquan">
                        <span>温馨提示：点击以下游戏场馆图标，可参与之对应的等级优惠。</span>
                        <div class="hjkp" style="margin-top:-10px;">
                            <div id="div-sb" class="hj-01">
                                <span>体育</span>
                                <p>SB</p>
                            </div>
                            <div id="div-hg" class="hj-02">
                                <span>锦绣厅</span>
                                <p>HG</p>
                            </div>
                            <div id="div-pt" class="hj-03">
                                <span>小游戏</span>
                                <p>PT</p>
                            </div>
                        </div>
                    </div>
                </div>
                <button type="button" id="transfer_submit" class="as_but inputwd300">立即转账</button>
            </form>

            <link rel="stylesheet" href="/js/loading/loading.css">
            <script src="/js/loading/loading.js"></script>
            <script th:inline="javascript">
                var promotionData = [[${promotionData}]];
            </script>
            <script>
                var jiuXiuLoad = new Loading();
                jiuXiuLoad.init();

                if (promotionData.pt.length > 0) {
                    $("#div-pt").addClass("hasPrice");
                } else {
                    $("#div-pt").addClass("nonePrice");
                }
                if (promotionData.sb.length > 0) {
                    $("#div-sb").addClass("hasPrice");
                } else {
                    $("#div-sb").addClass("nonePrice");
                }
                if (promotionData.hg.length > 0) {
                    $("#div-hg").addClass("hasPrice");
                } else {
                    $("#div-hg").addClass("nonePrice");
                }

                $(".hjkp .hj-01, .hjkp .hj-02, .hjkp .hj-03").click(function () {
                    $("#source-transfer").val("main");
                    $("#desc-transfer").val($(this).find("p").text());

                    getCouponsOfGame($(this).find("p").text());
                });

                $(".money_box").find("b").click(function () {
                    var amountMoney = $(this).text();
                    $("#transferMoney").val(amountMoney);
                });

                function outSelect() {
                    var currtSelected = $("#source-transfer option:selected").val();
                    $('#desc-transfer').find("option").siblings().show();
                    if (currtSelected == "main") {
                        $('#desc-transfer').find("option[value='main']").hide();
                        $("#desc-transfer option:last").prop("selected", 'selected');
                        getCouponsOfGame($("#desc-transfer").val());
                    } else {
                        $('#desc-transfer').find("option").siblings().hide();
                        $("#desc-transfer option[value='main']").show().prop("selected", 'selected');
                        $("#levelDiscounts").html("<option value=''>请选择</option>");
                    }
                }

                $("#source-transfer").change(function () {
                    outSelect();
                });

                $("#desc-transfer").change(function () {
                    let sParam = $(this).children('option:selected').val();
                    if (sParam != "main") {
                        getCouponsOfGame(sParam);
                    }
                });

                function getCouponsOfGame(gameType) {
                    jiuXiuLoad.start();
                    var tempData = [];
                    if (gameType == "PT") {
                        tempData = promotionData.pt;
                    } else if (gameType == "SB") {
                        tempData = promotionData.sb;
                    } else if (gameType == "HG") {
                        tempData = promotionData.hg;
                    }
                    if (tempData.length > 0) {
                        let html = "<option value=''>请选择</option>";
                        for (let i = 0; i < tempData.length; i++) {
                            html += "<option value='" + tempData[i].id + "'>" + tempData[i].name + "</option>"
                        }
                        $("#levelDiscounts").html(html);
                    }
                    jiuXiuLoad.stop();
                    /*jiuXiuLoad.start();
                    $.ajax({
                        method: "POST",
                        url: "/api/getCouponsOfGame",
                        data: {gameType: gameType},
                        dataType: 'json'
                    }).done(function (data) {
                        jiuXiuLoad.stop();
                        if (data.status == 'success') {
                            let html = "<option value=''>请选择</option>";
                            for (let i = 0; i < data.data.length; i++) {
                                html += "<option value='" + data.data[i].id + "'>" + data.data[i].name + "</option>"
                            }
                            $("#levelDiscounts").html(html);
                        } else {
                            alertWorning(data.data);
                        }
                    });*/
                }

                $("#transfer_submit").click(function () {
                    var source = $("#source-transfer option:selected").val();
                    var sourceValue = $("#source-transfer option:selected").text();
                    var desctination = $("#desc-transfer option:selected").val();
                    var desctinationValue = $("#desc-transfer option:selected").text();

                    if (source == desctination && source == 'main' && desctination == 'main') {
                        alertWorning('不能从' + sourceValue + "转入到" + desctinationValue);
                        return;
                    }

                    var transferMoneyvalue = $("#transferMoney").val();
                    if (transferMoneyvalue == "") {
                        alertWorning('请输入您的转账金额!');
                        return;
                    }

                    jiuXiuLoad.start();
                    $.ajax({
                        method: "POST",
                        url: "/api/transferSubmit",
                        data: $("#transferForm").serializeArray(),
                        dataType: 'json'
                    }).done(function (data) {
                        jiuXiuLoad.stop();
                        if (data.status == 'success') {
                            flushUserInfo();
                            alertGood("转账成功");
                            document.getElementById("transferForm").reset();
                            /*setTimeout(function () {
                                window.location.reload();
                            }, 2000);*/
                        } else {
                            alertWorning(data.data);
                        }
                    });
                });

                function flushUserInfo() {
                    jiuXiuLoad.start();
                    $.ajax({
                        method: "POST",
                        url: "/api/refreshUserInfo",
                        dataType: "json"
                    }).done(function (msg) {
                        jiuXiuLoad.stop();
                        if (msg.status == 'success') {
                            $("#MainBalance_b").html('<i>¥  </i><h>' + formatCurrency2(msg.data.userBalance) + '</h>');
                            $("#hg_balace").html(formatCurrency2(msg.data.hgBalance));
                        }
                    });
                }

                function formatCurrency2(num) {
                    if (!num || isNaN(num)) {
                        num = "0.00";
                    }
                    num = num.toString().replace(/\$|\,/g, '');
                    sign = (num == (num = Math.abs(num)));
                    num = Math.floor(num * 100 + 0.50000000001);
                    cents = num % 100;
                    num = Math.floor(num / 100).toString();
                    if (cents < 10)
                        cents = "0" + cents;
                    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
                        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
                    }
                    return (((sign) ? '' : '-') + num + '.' + cents);
                }

                outSelect();
            </script>
        </div>
    </div>
</div>
</body>
</html>
